<?php
/**
 * Created by PhpStorm.
 * User: mac1
 * Date: 16/6/10
 * Time: 上午10:32
 */

namespace app\admin\controller;
use app\admin\model\Admintype;
use app\admin\model\Campus;
use app\admin\validate\AdminValidate;
use think\Db;
use app\admin\model\Admin;

class ApitrainController extends CommonController
{

    /**
     * 给用户做题题目
     * $user_id
     * @return string
     */
    public function getTrains()
    {
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $yiju = $this->canIdo($user_id);
        if($yiju == true){
            $idfields = Db::table('ew_subject')->column('subject_id');
            if($idfields){
                $ids = $this->getNeedIds($idfields);
                $field = "subject_id subjectId,subject_a subjectA,subject_b subjectB";
                $field .= ",subject_c subjectC,subject_d subjectD,subject_answer subjectAnswer,subject_question subjectQuestion";
                $data = Db::table('ew_subject')
                    ->where('subject_id','in',$ids)
                    ->field($field)
                    ->select();
                return json_encode(['status'=>200,"msg"=>$data]);
            }else{
                $returnData['status'] = 0;
                $returnData['msg'] = "题库暂无数据，请期待！";
                return json_encode($returnData);
            }
        }else{
            $returnData['status'] = 0;
            $returnData['msg'] = "你今天已经达到答题次数了，请明天再来吧！";
            return json_encode($returnData);
        }
    }
	public function getTrainInfo()
    {
		header("Access-Control-Allow-Origin:*");
        extract(input());
        $field = "subject_id subjectId,subject_a subjectA,subject_b subjectB";
        $field .= ",subject_c subjectC,subject_d subjectD,subject_answer subjectAnswer,subject_question subjectQuestion";
        $data = Db::table('ew_subject')
            ->where('subject_id',$sid)
            ->field($field)
            ->find();
        return json_encode(['status'=>200,"msg"=>$data]);
    }

    /**
     * $score 用户得分
     * @return string
     */
    public function scoreResult()
    {
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $yiju = $this->canIdo($user_id);
        if($yiju == true){
            // 启动事务
            Db::startTrans();
            try{
                $today = date('Y-m-d');
                $data = array();
                $data['sc_user_id'] = $user_id;
                $data['sc_date'] = $today;
                Db::table('ew_sub_record')->insert($data);
                //------------------用户赞数增加------------------
                $inc = (int)($score/5);
                Db::table('ew_u_user')->where('user_id',$user_id)->setInc('user_zan',$inc);
                // 提交事务
                Db::commit();
                $returnData['status'] = 200;
                $returnData['msg'] = "成功";
                return json_encode($returnData);
            } catch (\Exception $e) {
                Db::rollback();
                return json_encode(['status'=>0,'msg'=>'失败']);
            }
        }else{
            $returnData['status'] = 0;
            $returnData['msg'] = "你今天已经达到答题次数了，请明天再来吧！";
            return json_encode($returnData);
        }
    }


    /**
     * 根据所有的题库的题的id返回10个随机id
     * @param $idfields
     * @return array
     */
    public function getNeedIds($idfields)
    {
        $num = count($idfields);
        $ids = array();
        $many = 10;
        if($num < 10){
            $many = $num;
        }
        $keys = array_rand($idfields,$many);
        foreach ($keys as $v){
            $ids[] = $idfields[$v];
        }
        return $ids;
    }


    /**
     * 判断此用户还能不能答题
     * $user_id
     * @return string
     */
    public function canIdo($user_id)
    {
        extract(input());
        $num = Db::table('ew_personalconfig')
            ->where('id',7)
            ->value('userid');
        $today = date('Y-m-d');
        $have = Db::table('ew_sub_record')
            ->where('sc_user_id',$user_id)
            ->where('sc_date',$today)
            ->count();
        if($have >= $num){
            return false;
        }else{
            return true;
        }
    }
}